let vm = new Vue({
    el: '#app',
    data: {
        username: '',
        password: '',
        code: '',
        render: 'kaptcha/render',
        user: {},
        conditional: {
            username: ''
        }
    },
    methods: {
        //点击更新验证码
        refresh: function () {
            this.render = `kaptcha/render?` + new Date();
        },
        index: function () {
            axios({
                url: `main/index`,
                params: {username: this.username, password: this.password, code: this.code}
            }).then(response => {
                if (!response.data.success) {
                    //失败
                    layer.msg(response.data.msg);

                    this.refresh();
                } else {
                    //成功
                    location.href = `user.html`;
                    //json转字符串
                    // let map = JSON.stringify(response.data.obj);
                    // sessionStorage.setItem("map",map)
                }
            }).catch(error => {
                layer.msg(error.message);
            })
        },
        sendEmail: function () {
            axios({
                url: `main/sendEmail`,
                params: this.conditional
            }).then(response => {
                layer.msg(response.data.msg);
            }).catch(error => {
                console.log(error.message)
            })
        },
        insert: function () {
            axios({
                url: "main/insert",
                method: "post",
                data: this.user
            }).then(response => {
                if (this.user.password == this.user.password2) {
                    // if (this.user.username == response.data.obj.username) {
                        if (response.data.success) {
                            //清空
                            this.user = {};
                        }
                        layer.msg("注册成功")
                    } else {
                        layer.msg("用户名已存在")
                    }
                // } else {
                //     layer.msg("密码不一致")
                // }
            }).catch(error => {
                layer.msg(error.message)
            })
        },
    }
});
